#include <stdio.h>
#include <sys/socket.h>
#include <error.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h> 
#define PORT 8888

int main() {
    int sock, new_sock;
    struct sockaddr_in my_addr, client_addr;
    int len;
    char buf1[100];
    char buf2[128];
    int recdata = 0;

    if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
        perror("socket create error!\n");
        exit(1);
    }

    memset(&my_addr, 0, sizeof(my_addr));
    my_addr.sin_family = AF_INET;
    my_addr.sin_port = htons(PORT);
    my_addr.sin_addr.s_addr = INADDR_ANY;

    if (bind(sock, (struct sockaddr *)&my_addr, sizeof(my_addr)) < 0) {
        perror("bind error\n");
        exit(1);
    }

    if (listen(sock, 5) < 0) {
        perror("listen error!\n");
        exit(1);
    }

    while (1) {
        len = sizeof(struct sockaddr);
        if ((new_sock = accept(sock, (struct sockaddr *)&client_addr, &len)) < 0) {
            perror("accept error!\n");
            exit(1);
        } else {
            printf("server get connection from %s, port %d, socket fd %d\n", inet_ntoa(client_addr.sin_addr), ntohs(client_addr.sin_port), new_sock);
            len = recv(new_sock, buf1, 100, 0);
            if (len < 0) {
                printf("recv error\n");
                exit(1);
            } else if (len == 0) {
                printf("the client quit!\n");
                break;
            } else {
                buf1[len] = '\0';
                printf("receive message: %s\n", buf1);
                recdata = atoi(buf1);
                recdata++;
                sprintf(buf2, "%d", recdata);
                if (send(new_sock, buf2, strlen(buf2), 0) < 0) {
                    perror("send data failed\n");
                }
            }
        }
    }

    close(sock);
    close(new_sock);
}